package installationInProgress;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class InstallationInProgress extends JFrame {

	private static final long serialVersionUID = 1L;
	private JProgressBar pgbInstall;
	private JButton btnPause;
	private JButton btnStop;
	int count;
	boolean blnPause;
	Timer tmrTimer;
	private JButton btnStart;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public InstallationInProgress() {
		initComponents();
		
		tmrTimer=new Timer(1000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(!blnPause){
					int temp=(int)(Math.random()*10);
					count=count+temp;
					
					pgbInstall.setValue(count);
					pgbInstall.setStringPainted(true);
				}
				
				if(count>=100){
					tmrTimer.stop();
					btnStop.setEnabled(false);
					btnStart.setEnabled(true);
					btnPause.setEnabled(false);
				}
			}
		});
	}

	private void initComponents() {
		setTitle("Installation in Project");
		setLayout(new GroupLayout());
		add(getBtnPause(), new Constraints(new Leading(117, 12, 12), new Leading(46, 10, 10)));
		add(getBtnStop(), new Constraints(new Leading(217, 10, 10), new Leading(46, 12, 12)));
		add(getPgbInstall(), new Constraints(new Leading(17, 300, 12, 12), new Leading(17, 10, 10)));
		add(getBtnStart(), new Constraints(new Leading(41, 10, 10), new Leading(43, 12, 12)));
		setSize(368, 95);
	}

	private JButton getBtnStart() {
		if (btnStart == null) {
			btnStart = new JButton();
			btnStart.setText("Start");
			btnStart.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					btnStartActionActionPerformed(event);
				}
			});
		}
		return btnStart;
	}


	private JButton getBtnStop() {
		if (btnStop == null) {
			btnStop = new JButton();
			btnStop.setText("Stop");
			btnStop.setPreferredSize(new Dimension(100, 20));
			btnStop.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					btnStopActionPerformed(arg0);
				}
			});
		}
		return btnStop;
	}

	protected void btnStopActionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		tmrTimer.stop();
		count=0;
		btnStop.setEnabled(false);
		btnStart.setEnabled(true);
		btnPause.setEnabled(false);
	}

	private JButton getBtnPause() {
		if (btnPause == null) {
			btnPause = new JButton();
			btnPause.setText("Pause");
			btnPause.setPreferredSize(new Dimension(100, 20));
			btnPause.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					btnPauseActionPerformed(arg0);
					
				}
			});
		}
		return btnPause;
	}


	protected void btnPauseActionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		blnPause=!blnPause;
		if(blnPause){
			btnPause.setText("Continue");
		}else{
			btnPause.setText("Pause");
		}
	}

	private JProgressBar getPgbInstall() {
		if (pgbInstall == null) {
			pgbInstall = new JProgressBar();
		}
		return pgbInstall;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				InstallationInProgress frame = new InstallationInProgress();
				frame.setDefaultCloseOperation(InstallationInProgress.EXIT_ON_CLOSE);
				frame.setTitle("InstallationInProgress");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void btnStartActionActionPerformed(ActionEvent event) {
		count=0;
		btnStop.setEnabled(true);
		btnStart.setEnabled(false);
		btnStop.setEnabled(true);
		tmrTimer.start();
	}

}
